<!--
 * @Author: your name
 * @Date: 2021-08-03 14:36:14
 * @LastEditTime: 2021-08-03 17:14:03
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>选择排序</title>
</head>

<body>
  <script>
    function selectSort(array) {
      const len = array.length
      for (let i = 0; i < len - 1; i++) {// 这里只需那倒数第二个数可以了，因为j=i+1拿取最后一个和倒数第二个比较
        for (let j = i + 1; j < len; j++) { // j = i + 1表示i和i+1比较，i和i++比较（第一轮，第一个[i]和第一[j]、第二[j]...比较）
          if (array[j] < array[i]) {
            [array[j], array[i]] = [array[i], array[j]]
          }
        }
      }
      return array
    }

    function selectSort1(array) {
      const len = array.length
      for (let i = 0; i < len - 1; i++) {
        for (let j = i + 1; j < len; j++) {
          if (array[i] > array[j]) {
            [array[i], array[j]] = [array[j], array[i]]
          }
        }
      }
      return array
    }


    console.log(selectSort1([1, 5, 2, 0, 7, 33, 5, 1, 67]))
    console.log(selectSort([1, 5, 2, 0, 7, 33, 5, 1, 67]))
  </script>
</body>

</html>